#include "common.h"
#include "mmap_file.h"
#include "mmap_file_op.h"
#include "index_handle.h"
#include <sstream>

using namespace std;
using namespace zsyuung_fs;

const static largefile::MMapOption mmap_option = 
{
    1024000,
    4096,
    4096
};

const static uint32_t main_blocksize = 1024 * 1024 * 64;
const static uint32_t bucket_size = 1000;

static int32_t block_id = 1;

static int debug1 = 1;

int main(int argc, char* argv[])
{
    string mainblock_path;
    string index_path;
    int32_t ret = largefile::TFS_SUCCESS;

    cout << "Input blockid:" << endl;
    cin >> block_id;

    if(block_id < 1)
    {
        cerr << "invalid blockid" << endl;
        exit(-1);
    }

    largefile::IndexHandle* index_handle = new largefile::IndexHandle(".", block_id);

    if(debug1)
    {
        printf("load index...\n");
    }

    ret = index_handle->load(block_id, bucket_size, mmap_option);

    if(ret != largefile::TFS_SUCCESS)
    {
        fprintf(stderr, "load index failed, desc:%s\n", strerror(errno));
        delete index_handle;
        exit(-2);
    }

    uint64_t file_id;
    cout << "input the filename" << endl;
    cin >> file_id;
  
    ret = index_handle->delete_segment_meta(file_id);
    if(ret != largefile::TFS_SUCCESS)
    {
        fprintf(stderr, "load delete failed, desc:%lu, %d\n", file_id, ret);
        delete index_handle;
        exit(-2);
    }   
   
    ret = index_handle->flush();
    if(ret != largefile::TFS_SUCCESS)
    {
        
        fprintf(stderr, "load delete failed, desc:%lu, %d\n", file_id, ret);
    }

    printf("delete successfully\n");
    
    delete index_handle;
    return 0;
}
